package cn.caplike.demo.grpc.proto.demo;

import cn.caplike.demo.grpc.proto.*;
import io.grpc.stub.StreamObserver;

import java.util.UUID;

/**
 * Description: 服务器端.
 *
 * @author LiKe
 * @version 1.0.0
 * @date 2021-12-11 12:27
 */
public class StudentServiceImpl extends StudentServiceGrpc.StudentServiceImplBase {

    @Override
    public void getRealNameByUsername(MyRequest request, StreamObserver<MyResponse> responseObserver) {
        System.out.println("Received client's message MyRequest#username: " + request.getUsername());

        responseObserver.onNext(MyResponse.newBuilder().setRealname("LiKe").build());
        responseObserver.onCompleted(); // 告诉客户端这个方法已经执行完毕
        System.out.println("-------------------------------------------------------------------");
    }

    @Override
    public void getStudentsByAge(StudentRequest request, StreamObserver<StudentResponse> responseObserver) {
        System.out.println("Received client's message StudentRequest#age: " + request.getAge());

        responseObserver.onNext(StudentResponse.newBuilder().setName("abc").setAge(18).build());
        responseObserver.onNext(StudentResponse.newBuilder().setName("def").setAge(19).build());
        responseObserver.onNext(StudentResponse.newBuilder().setName("ghi").setAge(17).build());
        responseObserver.onNext(StudentResponse.newBuilder().setName("jkl").setAge(16).build());
        responseObserver.onCompleted();
        System.out.println("-------------------------------------------------------------------");
    }

    @Override
    public StreamObserver<StudentRequest> getStudentsWrapperByAges(StreamObserver<StudentResponseList> responseObserver) {
        return new StreamObserver<StudentRequest>() {
            @Override
            public void onNext(StudentRequest value) {
                // ~ 接收到客户端的一次消息, onNext 就会调用一次
                System.out.println("onNext: " + value.getAge());
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.getMessage());
            }

            @Override
            public void onCompleted() {
                // ~ 服务端感知到客户端的数据发送结束事件
                // ~ 当客户端的流式数据全部发送给了服务端数据完成之后, 服务端要将结果返回给客户端
                StudentResponse studentResponse1 = StudentResponse.newBuilder().setName("2131h2").build();
                StudentResponse studentResponse2 = StudentResponse.newBuilder().setName("8273gu").build();

                final StudentResponseList studentResponseList =
                    StudentResponseList.newBuilder().addStudentResponses(studentResponse1).addStudentResponses(studentResponse2).build();

                // ~ 服务端返回与服务端结束
                responseObserver.onNext(studentResponseList);
                responseObserver.onCompleted();
                System.out.println("--------------------------------------------------------------------------------------------");
            }
        };
    }

    @Override
    public StreamObserver<StreamRequest> biTalk(StreamObserver<StreamResponse> responseObserver) {
        return new StreamObserver<StreamRequest>() {
            @Override
            public void onNext(StreamRequest value) {
                System.out.println("onNext: " + value.getRequestInfo());

                // 向客户端返回数据
                responseObserver.onNext(StreamResponse.newBuilder().setResponseInfo(UUID.randomUUID().toString()).build());
            }

            @Override
            public void onError(Throwable t) {
                System.err.println(t.getMessage());
            }

            @Override
            public void onCompleted() {
                // 客户端和服务端是两个相互独立的流: 客户端把流关闭了, 那么服务端也要把流关闭
                responseObserver.onCompleted();
            }
        };
    }
}
